import javax.swing.*;
import java.util.ArrayList;
import java.util.List;

public class Solution {
    List<List<Integer>> ret = new ArrayList<>(); //结果

    List<Integer> path = new ArrayList<>();//记录路径

    boolean[] flag;

    int len;

    public List<List<Integer>> permute(int[] nums) {
        len = nums.length;
        System.out.println(len);

        flag = new boolean[len];

        dfs(nums);

        return ret;

    }

    public void dfs(int[] nums) {
        //添加结果
        if (path.size() == len) {
            ret.add(new ArrayList<>(path));
            return;
        }

        for (int i = 0; i < len; i++) {
            if (!flag[i]) {
                path.add(nums[i]);
                flag[i] = true;
                dfs(nums);
                //回溯
                flag[i] = false;
                path.remove(path.size() - 1);
            }
        }
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        System.out.println(solution.permute(new int[]{1, 2, 3}));
    }
}
